/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.ateam.catastromobile.data;

import static com.ateam.catastromobile.FUTApplication.LOG_TAG;

import java.util.concurrent.ExecutionException;

import org.apache.http.HttpVersion;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

import com.ateam.catastromobile.FUTApplication;

import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor;
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
import org.jboss.resteasy.spi.ResteasyProviderFactory;

import com.ateam.catastromobile.entidades.*;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;

/**
 * 
 * @author thomas.diesler@jboss.com
 * @since 23-Aug-2011
 */
public class FUTResteasyClient implements FUTClient {

    private final Context context;
    private String lastRequestURI;
    private FUTClient client;
    //private Fut _fut;
    
    public FUTResteasyClient(Context context) {
        this.context = context;
        RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
    }

    @Override
    public Fut getFut(String isbn) {
//    	Fut result = null;
//        try {
//            result = getFutClient().getFut(isbn);
//        } catch (RuntimeException ex) {
//            ex.printStackTrace();
//        }
//        Log.i(LOG_TAG, "getFut: " + result);
//        return result;
    	Fut result = null;
        try {
        	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        		return new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, isbn).get();
        	else
        		return new MyAsyncTask().execute(isbn).get();
            
            //result = _fut;
        } catch (RuntimeException ex) {
            ex.printStackTrace();
        } catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        Log.i(LOG_TAG, "getFut: " + result);
        return result;
    }
    
    @Override
	public Usuario getUsuario(String isbn) {
    	Usuario result = null;
        try {
        	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        		return new MyAsyncTaskUser().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, isbn).get();
        	else
        		return new MyAsyncTaskUser().execute(isbn).get();
            
            //result = _fut;
        } catch (RuntimeException ex) {
            ex.printStackTrace();
        } catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        Log.i(LOG_TAG, "getFut: " + result);
        return result;
	}

    private FUTClient getFutClient() {
        String requestURI = FUTApplication.getRequestURI(context);
        if (client == null || !requestURI.equals(lastRequestURI)) {
            BasicHttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
            HttpProtocolParams.setUseExpectContinue(params, false);
            client = ProxyFactory.create(FUTClient.class, requestURI, new ApacheHttpClient4Executor(params));
            lastRequestURI = requestURI;
        }
        return client;
    }
    
    public class MyAsyncTask extends AsyncTask<String, Void, Fut>{

		@Override
		protected Fut doInBackground(String... params) {
			Fut result = null;
	        try {
	            result = getFutClient().getFut(params[0]);
	        } catch (RuntimeException ex) {
	            ex.printStackTrace();
	        }
	        Log.i(LOG_TAG, "getFut: " + result);
	        //_fut = result;
	        //return _fut;
	        return result;
		}
    	
//		protected void onPostExecute(Fut fut) {
//			_fut = fut;
//	    }
    	
    }
    
    public class MyAsyncTaskUser extends AsyncTask<String, Void, Usuario>{

		@Override
		protected Usuario doInBackground(String... params) {
			Usuario result = null;
	        try {
	            result = getFutClient().getUsuario(params[0]);
	        } catch (RuntimeException ex) {
	            ex.printStackTrace();
	        }
	        Log.i(LOG_TAG, "getFut: " + result);
	        //_fut = result;
	        //return _fut;
	        return result;
		}
    	
//		protected void onPostExecute(Fut fut) {
//			_fut = fut;
//	    }
    	
    }

}